<template>
  <div>
    <el-form ref="userForm" :model="userForm" :rules="rules" label-position="left" label-width="150px">
      <el-form-item label="姓名：" style="width:600px" prop="username">
        <el-input v-model="userForm.username" placeholder="1~30个字符" />
      </el-form-item>
      <el-form-item
        label="手机："
        style="width:500px"
        prop="mobile"
      >
        <el-input v-model="userForm.mobile" placeholder="请输入手机号" />
      </el-form-item>
      <el-form-item label="入职时间：" prop="timeOfEntry">
        <el-date-picker v-model="userForm.timeOfEntry" type="date" placeholder="选择日期" style="width: 30%;" />
      </el-form-item>
      <el-form-item label="聘用形式：" prop="formOfEmployment">
        <el-select v-model="userForm.formOfEmployment" placeholder="请选择">
          <el-option label="正式" value="1" />
          <el-option label="非正式" value="2" />
        </el-select>
      </el-form-item>
      <el-form-item label="工号：" style="width:600px" prop="workNumber">
        <el-input v-model="userForm.workNumber" placeholder="1~20个字符" />
      </el-form-item>
      <el-form-item label="部门：" style="width:500px" prop="departmentName">
        <el-input v-model="userForm.departmentName" placeholder="请选择" @focus="departfocus" />
        <el-row ref="treeData" class="treeData">
          <el-tree :data="departall" :props="{label:'name'}" default-expand-all @node-click="hNodeClick" />
        </el-row>
      </el-form-item>
      <el-form-item label="转正时间：" prop="correctionTime">
        <el-date-picker v-model="userForm.correctionTime" type="date" placeholder="选择日期" style="width: 30%;" />
      </el-form-item>
    </el-form>
    <el-row type="flex" justify="center">
      <el-button type="primary" @click="subMit">提 交</el-button>
      <el-button @click="addVisible = false">取 消</el-button>
    </el-row>
  </div>
</template>
<script>
import { validMobile } from '@/utils/validate'
import { addUserDetails } from '@/api/employees'
export default {
  name: '',
  props: {
    departall: {
      type: Array,
      required: true
    }
  },
  data() {
    var validphone = (rule, value, callback) => {
      if (validMobile(value)) {
        callback()
      } else {
        callback(new Error('手机号格式有误'))
      }
    }
    return {
      userForm: {
        username: '',
        mobile: '',
        timeOfEntry: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        correctionTime: ''
      },
      rules: {
        username: [{ required: true, message: '姓名项不能为空', trigger: 'blur' }],
        mobile: [{ required: true, message: '手机号不能为空', trigger: 'blur' }, { validator: validphone, message: '手机号格式有误', trigger: 'blur' }],
        timeOfEntry: [{ required: true, message: '请选择入职时间', trigger: 'blur' }],
        formOfEmployment: [{ required: true, message: '请选择聘用形式', trigger: 'change' }],
        workNumber: [{ required: true, message: '工号不能为空', trigger: 'blur' }],
        departmentName: [{ required: true, message: '请选择部门', trigger: 'change' }],
        correctionTime: [{ required: true, message: '请选择转正时间', trigger: 'blur' }]
      }
    }
  },
  computed: {
  },
  created() {
  },
  methods: {
    departfocus() {
      this.$refs.treeData.$el.style.display = 'block'
    },
    // departblur() {
    //   this.$refs.treeData.$el.style.display = 'none'
    // },
    hNodeClick(data) {
      if (data.children) return
      this.userForm.departmentName = data.name
      this.$refs.treeData.$el.style.display = 'none'
    },
    subMit() {
      this.$refs.userForm.validate(async valid => {
        if (!valid) return
        const res = await addUserDetails(this.userForm)
        try {
          this.$message({
            type: 'success',
            message: res.message
          })
          this.$refs.userForm.resetFields()
          //   通知父组件关闭弹框
          this.$emit('success')
        } catch (err) {
          console.log(err)
        }
      })
    }
  }
}
</script>
<style lang='scss'  scoped>
.treeData{
    position: relative;
    margin-top: 5px;
    display: none;
}
.el-tree{
    width: 100%;
  border: 1px solid #dddee1;
  position: absolute;
  border-radius:5px ;
  z-index: 999;
}
</style>
